package cn.itcast.travel.web.servlet.blogger;

import cn.itcast.travel.domain.blogger;
import cn.itcast.travel.service.BloggerService;
import cn.itcast.travel.service.impl.BloggerServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

//用户注册
@WebServlet(name = "register", value = "/register")
public class register extends HttpServlet {
    BloggerService service=new BloggerServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map mp=request.getParameterMap();
        blogger b1=new blogger();
        try {
            BeanUtils.populate(b1,mp);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        boolean b2=service.register(b1);
        ObjectMapper ob=new ObjectMapper();
        if(!b2){
            System.out.println(1);
            ob.writeValue(response.getWriter(),null);
        }else{
            System.out.println(2);
            ob.writeValue(response.getWriter(),1);
        }
    }
}
